package com.study.news.controls;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.study.news.dao.CommentDao;
import com.study.news.dao.NewsDAO;
import com.study.news.pojo.Comment;
import com.study.news.pojo.News;
import com.study.news.utils.CommonUtils;

@WebServlet("/new")
public class DetailServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	
		//从页面获取参数
		String pp=request.getParameter("id");
		NewsDAO dao=new NewsDAO();
		Integer newId=0;
		if(CommonUtils.verifyPageNo(pp)) {
			newId=Integer.parseInt(pp);
			News obj = dao.selectById(newId);
			System.out.println(obj);
			request.setAttribute("newsDetail", obj);
			CommentDao commentDao = new CommentDao();
			List<Comment> commentList =  commentDao.getCommentList(newId);
			
			request.setAttribute("commentList", commentList);
			request.getRequestDispatcher("WEB-INF/lesson9/new.jsp").forward(request, response);
		}else {
			//错误提示
			//可以将错误信息存储在作用域中，然后显示在error页面
			response.sendRedirect("WEB-INF/error.jsp");
		}
	}
}
